#include <iostream>
#include <fstream>
#include <string>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/Appender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/Category.hh>
using std::cout;
using std::endl;
using std::ios;
using std::string;
using namespace log4cpp;

// 使用log4cpp设计布局合理的日志系统，将日志信息输出到终端，保存到文件，保存到回卷文件

int main(void)
{
    // 格式化器
    PatternLayout* ptn1 = new PatternLayout ();
    ptn1->setConversionPattern("%d %c [%p] %m %n");
    PatternLayout* ptn2 = new PatternLayout ();
    ptn2->setConversionPattern("%d %c [%p] %m %n");
    PatternLayout* ptn3 = new PatternLayout ();
    ptn3->setConversionPattern("%d %c [%p] %m %n");


    // 输出器
    Appender* appender1 = new FileAppender("File", "file.log");
    appender1->setLayout(ptn1);
    Appender* appender2 = new OstreamAppender("Console", &cout);
    appender1->setLayout(ptn2);
    Appender* appender3 = new RollingFileAppender("Roll", "rool.log", 4 * 1024, 9);
    appender1->setLayout(ptn3);
    
    // 记录器
    Category& sub1 = Category::getInstance(string("sub1"));
    sub1.addAppender(appender1);
    Category& sub2 = Category::getInstance(string("sub2"));
    sub2.addAppender(appender2);
    Category& sub3 = Category::getInstance(string("sub3"));
    sub3.addAppender(appender3);

    // 过滤器
    sub1.setPriority(Priority::DEBUG);
    sub2.setPriority(Priority::DEBUG);
    sub3.setPriority(Priority::DEBUG);


    int count = 30;
    while(count--){
        sub1.error("this is error");
        sub1.info("this is info");
        sub1.warn("this is warn");
        sub1.debug("this is debug");
        sub1.crit("this is crit");

        sub2.error("this is error");
        sub2.info("this is info");
        sub2.warn("this is warn");
        sub2.debug("this is debug");
        sub2.crit("this is crit");

        sub3.error("this is error");
        sub3.info("this is info");
        sub3.warn("this is warn");
        sub3.debug("this is debug");
        sub3.crit("this is crit");
    }

    Category::shutdown();
    return 0;
}